package com.example.student.controller;

import com.example.student.common.Result;
import com.example.student.entity.SysRole;
import com.example.student.model.PageResult;
import com.example.student.service.RoleService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;

@RestController
@RequestMapping("/api/roles")
public class RoleController {

    @Resource
    private RoleService roleService;

    /**
     * 获取角色列表（分页）
     */
    @GetMapping("/list")
    @PreAuthorize("hasAuthority('system:role:list')")
    public Result<PageResult<SysRole>> list(
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "10") Integer pageSize,
            @RequestParam(required = false) String roleName,
            @RequestParam(required = false) String roleCode) {
        return Result.success(roleService.getRoleList(page, pageSize, roleName, roleCode));
    }

    /**
     * 获取所有角色
     */
    @GetMapping("/all")
    @PreAuthorize("hasAuthority('system:role:list')")
    public Result<List<SysRole>> getAllRoles() {
        return Result.success(roleService.getAllRoles());
    }
    
    /**
     * 获取所有可用角色（用于用户分配角色）
     */
    @GetMapping("/available")
    @PreAuthorize("hasAuthority('system:user:edit')")
    public Result<List<SysRole>> getAvailableRoles() {
        return Result.success(roleService.getAvailableRoles());
    }

    /**
     * 获取角色详情
     */
    @GetMapping("/{id}")
    @PreAuthorize("hasAuthority('system:role:list')")
    public Result<SysRole> getById(@PathVariable Long id) {
        return Result.success(roleService.getRoleById(id));
    }

    /**
     * 添加角色
     */
    @PostMapping
    @PreAuthorize("hasAuthority('system:role:add')")
    public Result<Void> add(@Valid @RequestBody SysRole role) {
        roleService.addRole(role);
        return Result.success();
    }

    /**
     * 更新角色
     */
    @PutMapping("/{id}")
    @PreAuthorize("hasAuthority('system:role:edit')")
    public Result<Void> update(@PathVariable Long id, @Valid @RequestBody SysRole role) {
        role.setId(id);
        roleService.updateRole(role);
        return Result.success();
    }

    /**
     * 删除角色
     */
    @DeleteMapping("/{id}")
    @PreAuthorize("hasAuthority('system:role:delete')")
    public Result<Void> delete(@PathVariable Long id) {
        roleService.deleteRole(id);
        return Result.success();
    }

    /**
     * 分配菜单权限
     */
    @PutMapping("/{roleId}/menus")
    @PreAuthorize("hasAuthority('system:role:edit')")
    public Result<Void> assignMenus(@PathVariable Long roleId, @RequestBody List<Long> menuIds) {
        roleService.assignMenus(roleId, menuIds);
        return Result.success();
    }

    /**
     * 获取角色的菜单ID列表
     */
    @GetMapping("/{roleId}/menus")
    @PreAuthorize("hasAuthority('system:role:edit')")
    public Result<List<Long>> getMenuIdsByRoleId(@PathVariable Long roleId) {
        return Result.success(roleService.getMenuIdsByRoleId(roleId));
    }

    /**
     * 获取用户的角色列表
     */
    @GetMapping("/user/{userId}")
    @PreAuthorize("hasAuthority('system:user:edit')")
    public Result<List<SysRole>> getRolesByUserId(@PathVariable Long userId) {
        return Result.success(roleService.getRolesByUserId(userId));
    }
} 